﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using libInventory.BUS;
using libCommonTools.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using libDAL;

namespace libInventory.FORMS
{
    public partial class frmInlandOriginDestination : Telerik.WinControls.UI.RadForm
    {
        private clsBUSInland _BUS;
        public frmInlandOriginDestination()
        {
            InitializeComponent();
            _BUS = new clsBUSInland();
        }

        private void set_DeactivateEvents()
        {
            this.mcmbPorts.SelectedIndexChanged -= new System.EventHandler(this.mcmbPorts_SelectedIndexChanged);
            this.mcmbPorts.SelectedValueChanged -= new System.EventHandler(this.mcmbPorts_SelectedValueChanged);
            this.mcmbPorts.TextChanged -= new System.EventHandler(this.mcmbPorts_TextChanged);
            this.dgvAdditionalChargesDestination.CurrentRowChanged -= new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesDestination_CurrentRowChanged);
            this.dgvAdditionalChargesDestination.CellClick -= new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvAdditionalChargesDestination_CellClick);
      
        }

        private void set_ActivateEvents()
        {
            this.mcmbPorts.SelectedIndexChanged += new System.EventHandler(this.mcmbPorts_SelectedIndexChanged);
            this.mcmbPorts.SelectedValueChanged += new System.EventHandler(this.mcmbPorts_SelectedValueChanged);
            this.mcmbPorts.TextChanged += new System.EventHandler(this.mcmbPorts_TextChanged);
            this.dgvAdditionalChargesDestination.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesDestination_CurrentRowChanged);
            this.dgvAdditionalChargesDestination.CellClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvAdditionalChargesDestination_CellClick);
        }

        private void frmInlandOriginDestination_Load(object sender, EventArgs e)
        {
            try
            {
                set_DeactivateEvents();
                set_LoadPorts();
                set_ActivateEvents();
                
                set_SelectedIndexValueTextChanged();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " frmInlandOriginDestination_Load"); 
            }
        }

        private void set_LoadPorts()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = _BUS.get_Ports(); 
                mcmbPorts.EditorControl.LoadElementTree();
                mcmbPorts.DataSource = dt;

                for (int intIndex = 0; intIndex < dt.Columns.Count; intIndex++)
                {
                    mcmbPorts.MultiColumnComboBoxElement.Columns[intIndex].BestFit();
                }

                mcmbPorts.MultiColumnComboBoxElement.DropDownWidth = 300;
                mcmbPorts.MultiColumnComboBoxElement.DropDownHeight = 150;
                mcmbPorts.SelectedIndex = -1;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadPortDestination");
            }
        }

        private DataTable get_GridColumns()
        {
            DataTable dtColumns = new DataTable();
            try
            {
                for (int intCol = 0; intCol < dgvAdditionalChargesDestination.Columns.Count; intCol++)
                { 
                    dtColumns.Columns.Add(dgvAdditionalChargesDestination.Columns[intCol].Name); 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_GridColumns"); 
            }
            return dtColumns;
        }

        private bool set_TransferFromGridToDataTable(ref DataTable dtStorage)
        {
            bool bolStatus = false;

            try
            {
                if (mcmbPorts.Text.Trim() != string.Empty)
                {
                    DataRow drInsert;
                    for (int intRow = 0; intRow < dgvAdditionalChargesDestination.Rows.Count; intRow++)
                    {
                        drInsert = dtStorage.NewRow();
                        for (int intCol = 0; intCol < dtStorage.Columns.Count; intCol++)
                        {
                            drInsert[dtStorage.Columns[intCol].ColumnName] = dgvAdditionalChargesDestination.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value;
                        }
                        drInsert["PortID"] = mcmbPorts.Text.Trim();
                        drInsert["InLandSetupChargesID"] = get_InLandChargesID(drInsert["PostCode"].ToString());
                        dtStorage.Rows.Add(drInsert);
                    }
                    bolStatus = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_TransferFromGridToDataTable"); 
            }
            return bolStatus;
        }



        public void set_SaveInlandSettings()
        {
            try
            {
                DataTable dtStorage = new DataTable();
                dtStorage = get_GridColumns();

                //note: during adding of rows per port, it must check in db for duplication

                if (set_TransferFromGridToDataTable(ref dtStorage) == true)
                {
                    if (_BUS.set_UpdateInsertToDataSet(dtStorage) == true)
                    { 
                        
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_SaveInlandSettings"); 
            }
        }

        private void dgvAdditionalChargesDestination_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
                { 
                    using (frmFindCompany find = new frmFindCompany("Area"))
                    {
                      DialogResult dr =   find.ShowDialog();
                      if (dr == DialogResult.OK)
                      {
                          dgvAdditionalChargesDestination.CurrentRow.Cells["PostCode"].Value = find.get_Code;
                          dgvAdditionalChargesDestination.CurrentRow.Cells["AreaDescription"].Value = find.get_Description; 
                      }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvAdditionalChargesDestination_CellClick"); 
            }
        }

        private void mcmbPorts_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (IsLoaded == true)
                {
                   // set_SelectedIndexValueTextChanged();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " mcmbPorts_SelectedIndexChanged"); 
            }
        }

        private void set_SelectedIndexValueTextChanged()
        {
            try
            {
                if (dgvAdditionalChargesDestination.Rows.Count > 0)
                {
                    dgvAdditionalChargesDestination.Rows.Clear();
                }

                GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(dgvAdditionalChargesDestination.MasterView);
                DataRow[] drGet = _BUS.get_AreasByPortFromDB(mcmbPorts.Text.Trim ());
                if(drGet.Length > 0)
                {
                    for (int intRow = 0; intRow < drGet.Length; intRow++)
                    {
                        rowInfo = new GridViewDataRowInfo(dgvAdditionalChargesDestination.MasterView);
                        for (int intCol = 0; intCol < dgvAdditionalChargesDestination.Columns.Count; intCol++)
                        {
                            rowInfo.Cells[dgvAdditionalChargesDestination.Columns[intCol].Name].Value = drGet[intRow][dgvAdditionalChargesDestination.Columns[intCol].Name];
                        }
                        dgvAdditionalChargesDestination.Rows.Add(rowInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_SelectedIndexValueTextChanged"); 
            }
        }

        private void mcmbPorts_SelectedValueChanged(object sender, EventArgs e)
        {
            if (IsLoaded == true)
            {
                set_SelectedIndexValueTextChanged();
            }
        }

        private void mcmbPorts_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvAdditionalChargesDestination_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            //InLandSetupChargesID
            //PortID
           
        }

        private string get_InLandChargesID(string strPostCode)
        {
            return mcmbPorts.Text.Trim() + "_" + strPostCode + "-" + clsGenFunction.get_GenKeyNEWID(_BUS.get_DAL);  
        }

        private void dgvAdditionalChargesDestination_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            if (mcmbPorts.Text.Trim() != string.Empty)
            {
                //dgvAdditionalChargesDestination.CurrentRow.Cells["InLandSetupChargesID"].Value = get_InLandChargesID();
                //dgvAdditionalChargesDestination.CurrentRow.Cells["PortID"].Value = mcmbPorts.Text.Trim();
            }
        }
    }
}
